using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace PlanetWPF.Toolbelt.Controls
{
    /// <summary>
    /// Represents the Header Row in a <see cref="PowerGrid"/>.
    /// </summary>
    public class PowerGridColumnHeaderRow : PowerGridRow
    {

        public int sortorder;
        static PowerGridColumnHeaderRow()
        {
            //This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
            //This style is defined in themes\generic.xaml
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PowerGridColumnHeaderRow), new FrameworkPropertyMetadata(typeof(PowerGridColumnHeaderRow)));
        }

        public void ColClicked(Object sender, RoutedEventArgs args)
        {
            int clickCount = 1;
            string previousHeader = string.Empty;
            PowerGridHeaderCell header = sender as PowerGridHeaderCell;
            string path = header.CellPath;
            PowerGrid view = TemplatedParent as PowerGrid;
            PowerGridView gridView = view.Template.FindName("PART_Grid", view) as PowerGridView;
            ItemsControl ic = gridView.Template.FindName("PART_ItemsControl", gridView) as ItemsControl;
            CollectionView cv = ic.Items;
            //Sort the data based on the column selected
            cv.SortDescriptions.Clear();
            ListSortDirection direction = (ListSortDirection)sortorder;
            if (direction == ListSortDirection.Ascending)
            {
                direction = ListSortDirection.Descending;
                sortorder = 1;
            }
            else
            {
                direction = ListSortDirection.Ascending;
                sortorder = 0;
            }
            cv.SortDescriptions.Add(new SortDescription(path, direction));
            clickCount++;
        }
    }
}
